home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / lib_std / basic_time.e < prev    next >
Text File  |  2000-03-25  |  8KB  |  288 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. expanded class BASIC_TIME
  13. --
  14. -- NOTE: THIS IS AN ALPHA VERSION. THIS CLASS IS NOT STABLE AT ALL AND 
  15. -- MIGHT EVEN CHANGE COMPLETELY IN THE NEXT RELEASE !
  16. -- 
  17. --
  18. -- Basic time and date facilities.
  19. --
  20.  
  21. inherit HASHABLE; COMPARABLE;
  22.  
  23. feature
  24.  
  25.    is_local_time: BOOLEAN is
  26.          -- Is the local time in use ?
  27.          -- This information applies to all objects of this class.
  28.       do
  29.          Result := (time_mode = 0);
  30.       ensure
  31.          Result implies not is_universal_time
  32.       end;
  33.    
  34.    is_universal_time: BOOLEAN is
  35.          -- Is Universal Time in use ?
  36.          -- This information applies to all objects of this class.
  37.       do
  38.          Result := (time_mode = 1);
  39.       ensure
  40.          Result implies not is_local_time
  41.       end;
  42.    
  43.    year: INTEGER is
  44.          -- Number of the year.
  45.       do
  46.          Result := basic_time_getyear(time_memory,time_mode);
  47.       end;
  48.    
  49.    month: INTEGER is
  50.          -- Number of the month (1 for January, 2 for February, ... 
  51.          -- 12 for December).
  52.       do
  53.          Result := basic_time_getmonth(time_memory,time_mode);
  54.       ensure
  55.          Result.in_range(1,12)
  56.       end;
  57.    
  58.    day: INTEGER is
  59.          -- Day of the `month' in range 1 .. 31.
  60.       do
  61.          Result := basic_time_getday(time_memory,time_mode);
  62.       ensure
  63.          Result.in_range(1,31)
  64.       end;
  65.    
  66.    hour: INTEGER is
  67.          -- Hour in range 0..23.
  68.       do
  69.          Result := basic_time_gethour(time_memory,time_mode);
  70.       ensure
  71.          Result.in_range(0,23)
  72.       end;
  73.    
  74.    minute: INTEGER is
  75.          -- Minute in range 0 .. 59.
  76.       do
  77.          Result := basic_time_getminute(time_memory,time_mode);
  78.       ensure
  79.          Result.in_range(0,59)
  80.       end;
  81.    
  82.    second: INTEGER is
  83.          -- Second in range 0 .. 59.
  84.       do
  85.          Result := basic_time_getsecond(time_memory, time_mode)
  86.       ensure
  87.          Result.in_range(0,59)
  88.       end;
  89.  
  90.    week_day: INTEGER is
  91.          -- Number of the day in the week (Sunday is 0, Monday is 1, etc.).
  92.       do
  93.          Result := basic_time_getwday(time_memory,time_mode);
  94.       ensure
  95.          Result.in_range(0,6)
  96.       end;
  97.    
  98.    year_day: INTEGER is
  99.          -- Number of the day in the year in range 0 .. 365.
  100.       do
  101.          Result := basic_time_getyday(time_memory,time_mode);
  102.       end;
  103.    
  104.    is_summer_time_used: BOOLEAN is
  105.          -- Is summer time in effect?
  106.       do
  107.          Result := basic_time_is_summer_time_used(time_memory);
  108.       end;
  109.    
  110. feature -- Setting :
  111.  
  112.    update is
  113.          -- Update `Current' with the current system clock.
  114.       do
  115.          time_memory := basic_time_time;
  116.       end;
  117.  
  118.    set(a_year, a_month, a_day, a_hour, a_min, sec: INTEGER): BOOLEAN is
  119.          -- Try to set `Current' using the given information. If this input
  120.          -- is not a valid date, the `Result' is false and `Current' is not updated.
  121.       local
  122.          time_mem: INTEGER;
  123.       do
  124.          time_mem := basic_time_mktime(a_year,a_month,a_day,a_hour,a_min,sec);
  125.          if time_mem /= -1 then
  126.             Result := true;
  127.             time_memory := time_mem;
  128.          end;
  129.       ensure
  130.          Result implies (year = a_year);
  131.          Result implies (month = a_month);
  132.          Result implies (day = a_day);
  133.          Result implies (hour = a_hour);
  134.          Result implies (minute = a_min);
  135.          Result implies (second = sec);
  136.          not Result implies Current = old Current
  137.       end;
  138.  
  139. feature
  140.    
  141.    elapsed_seconds(other: like Current): INTEGER is
  142.          -- Elapsed time in seconds from `Current' to `other'.
  143.       require
  144.          Current <= other
  145.       do
  146.          Result := basic_time_difftime(other.time_memory,time_memory);
  147.       ensure
  148.          Result >= 0
  149.       end;
  150.  
  151.    infix "<" (other: like Current): BOOLEAN is
  152.       local
  153.          v1, v2: INTEGER;
  154.       do
  155.          v1 := year;
  156.          v2 := other.year;
  157.          if v1 < v2 then
  158.             Result := true;
  159.          elseif v1 = v2 then
  160.             v1 := year_day;
  161.             v2 := other.year_day;
  162.             if v1 < v2 then
  163.                Result := true;
  164.             elseif v1 = v2 then
  165.                v1 := hour;
  166.                v2 := other.hour;
  167.                if v1 < v2 then
  168.                   Result := true;
  169.                elseif v1 = v2 then
  170.                   v1 := minute;
  171.                   v2 := other.minute;
  172.                   if v1 < v2 then
  173.                      Result := true;
  174.                   elseif v1 = v2 then
  175.                      Result := second < other.second;
  176.                   end;
  177.                end;
  178.             end;
  179.          end;
  180.       end;
  181.    
  182. feature -- Setting common time mode (local or universal) :
  183.    
  184.    set_universal_time is
  185.       do
  186.          time_mode_memo.set_item(1);
  187.       ensure
  188.          is_universal_time
  189.       end;
  190.    
  191.    set_local_time is
  192.       do
  193.          time_mode_memo.set_item(0);
  194.       ensure
  195.          is_local_time
  196.       end;
  197.  
  198. feature
  199.  
  200.    clock_periods: INTEGER is
  201.          -- CPU time usage if available (-1 if not).
  202.       do
  203.          Result := basic_time_clock;
  204.       end;
  205.  
  206. feature -- Hashing :
  207.  
  208.    hash_code: INTEGER is
  209.       do
  210.          Result := time_memory.hash_code;
  211.       end;
  212.    
  213. feature {BASIC_TIME}
  214.  
  215.    time_memory : INTEGER
  216.          -- The current time value of `Current'. As far as I know, this 
  217.          -- kind of information fits on 32 bits for all platforms.
  218.    
  219. feature {NONE}
  220.  
  221.    time_mode_memo: MEMO[INTEGER] is
  222.          -- The global default `time_mode' memory.
  223.       once
  224.          !!Result;
  225.       end;
  226.    
  227.    time_mode: INTEGER is
  228.       do
  229.          Result := time_mode_memo.item;
  230.       ensure
  231.          Result = 0 or Result = 1
  232.       end;
  233.  
  234.    basic_time_time : INTEGER is
  235.       external "SmallEiffel"
  236.       end;
  237.    
  238.    basic_time_difftime (time2, time1 : INTEGER) : INTEGER is
  239.       external "SmallEiffel"
  240.       end;
  241.    
  242.    basic_time_getyear (tm: INTEGER; mode: INTEGER): INTEGER is
  243.       external "SmallEiffel"
  244.       end;
  245.    
  246.    basic_time_getmonth (tm: INTEGER; mode: INTEGER): INTEGER is
  247.       external "SmallEiffel"
  248.       end;
  249.    
  250.    basic_time_getday (tm: INTEGER; mode: INTEGER): INTEGER is
  251.       external "SmallEiffel"
  252.       end;
  253.    
  254.    basic_time_gethour (tm: INTEGER; mode: INTEGER): INTEGER is
  255.       external "SmallEiffel"
  256.       end;
  257.    
  258.    basic_time_getminute(tm: INTEGER; mode: INTEGER): INTEGER is
  259.       external "SmallEiffel"
  260.       end;
  261.    
  262.    basic_time_getsecond(tm: INTEGER; mode: INTEGER): INTEGER is
  263.       external "SmallEiffel"
  264.       end;
  265.    
  266.    basic_time_is_summer_time_used(tm: INTEGER): BOOLEAN is
  267.       external "SmallEiffel"
  268.       end;
  269.    
  270.    basic_time_getyday(tm: INTEGER; mode: INTEGER): INTEGER is
  271.       external "SmallEiffel"
  272.       end;
  273.    
  274.    basic_time_getwday(tm: INTEGER; mode: INTEGER): INTEGER is
  275.       external "SmallEiffel"
  276.       end;
  277.    
  278.    basic_time_mktime(a_year, a_mon, a_day, 
  279.                      a_hour, a_min, a_sec: INTEGER): INTEGER is
  280.       external "SmallEiffel"
  281.       end;
  282.    
  283.    basic_time_clock: INTEGER is
  284.       external "SmallEiffel"
  285.       end;
  286.    
  287. end -- BASIC_TIME
  288.